package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.BuSysPerson;
import com.ruoyi.common.core.domain.entity.Person;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.jm.JMJUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.domain.LoginSysUserDep;
import com.ruoyi.system.mapper.SysConfigMapper;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.service.IBuSysPersonService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.JmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.PersonMapper;
import com.ruoyi.system.service.IPersonService;

/**
 * 基础人员Service业务层处理
 *
 * @author ess
 * @date 2021-10-14
 */
@Service
public class PersonServiceImpl implements IPersonService {
    @Autowired
    private PersonMapper personMapper;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IBuSysPersonService buSysPersonService;
    @Autowired
    private JmService jmService;
    /**
     * 查询基础人员
     *
     * @param personId 基础人员主键
     * @return 基础人员
     */
    @Override
    public Person selectPersonByPersonId(String personId) {
//        返回解密
        return getDPerson(personMapper.selectPersonByPersonId(personId));
    }

    /**
     * 查询基础人员列表
     *
     * @param person 基础人员
     * @return 基础人员
     */
    @Override
    public List<Person> selectPersonList(Person person) {
//        参数加密
        List<Person> people = personMapper.selectPersonList(getEPerson(person));
//       返回解密
        if (people != null && people.size() > 0) {
            for (Person personE : people) {
                getDPerson(personE);
            }
            return people;
        }

        return people;
    }

    /**
     * 新增基础人员
     *
     * @param person 基础人员
     * @return 结果
     */
    @Override
    public int insertPerson(Person person) {
        String personIdUUid = UUID.fastUUID().toString(true);
//       设置 personid uuid
        person.setPersonId(personIdUUid);
//        设置人员编码
        person.setEmpNum(personIdUUid);
//        设置登录账号
        person.setLoginAccount(person.getPersonName());
//       设置状态
        person.setState(0L);

//        person.setSex(1L);

//        参数加密
        return personMapper.insertPerson(getEPerson(person));
    }

    /**
     * 修改基础人员
     *
     * @param person 基础人员
     * @return 结果
     */
    @Override
    public int updatePerson(Person person) {
//        当有用户时也修改用户中的用户姓名 保持一致
//        sysUserService.updateUserNameByPersonId(ePerson);
//        参数加密
        Person ePerson = getEPerson(person);
        return personMapper.updatePerson(ePerson);
    }

    /**
     * 批量删除基础人员
     *
     * @param personIds 需要删除的基础人员主键
     * @return 结果
     */
    @Override
    public int deletePersonByPersonIds(String[] personIds) {
//        删除对应用户数据 授权
        for (int i = 0; i < personIds.length; i++) {
            SysUser sysUser = new SysUser();
            sysUser.setPersonId(personIds[i]);
            List<SysUser> sysUserList = sysUserService.selectUserList(sysUser);
            if (sysUserList != null && sysUserList.size() > 0) {
                for (SysUser user : sysUserList) {
                    sysUserService.deleteUserById(user.getUserId());
                }

                personMapper.deletePersonByPersonId(personIds[i]);


            } else {
                personMapper.deletePersonByPersonId(personIds[i]);
            }

        }
        return 1;
    }

    /**
     * 删除基础人员信息
     *
     * @param personId 基础人员主键
     * @return 结果
     */
    @Override
    public int deletePersonByPersonId(String personId)
    {
        return personMapper.deletePersonByPersonId(personId);
    }


    /**
     * 根据姓名查询基础人员列表
     *
     * @param personName
     * @return
     */
    @Override
    public List<Person> selectPersonListByPersonName(String personName) {
        //        姓名加密
        String personNameE = jmService.getEncryptString(personName);

        List<Person> people = personMapper.selectPersonListByPersonName(personNameE);
        //        返回解密
        if (people != null && people.size() > 0) {
            for (Person personE : people) {
                getDPerson(personE);
            }
            return people;
        }

        return people;
    }

    @Override
    public Person findPersonBySYS(String businessSys, String signUser) {
        BuSysPerson buSysPerson = buSysPersonService.selectBusysByBuSysPerId(signUser);
        if (businessSys==null){
            return null;
        }
        return selectPersonByPersonId(buSysPerson.getPersonId());
    }
    /**
     * 查询同一单位下的基础人员
     * @param personName
     * @param user
     * @return
     */
    @Override
    public List<Person> selectPersonWithSysDepListByPersonName(String personName, SysUser user) {
        //        姓名加密
        String personNameE = jmService.getEncryptString(personName);
        List<Person> people = personMapper.selectPersonWithSysDepListByPersonName(personNameE,user.getDeptId());
        //        返回解密
        if (people != null && people.size() > 0) {
            for (Person personE : people) {
                getDPerson(personE);
            }
            return people;
        }

        return people;
    }

    @Override
    public int resetPwd(Person person) {
//        加密
        return personMapper.upresetPwd(person.getPersonId(),jmService.getEncryptString(person.getLoginPsw()));
    }

    /**
     * 重置密码 个人中心
     *
     * @param userId 用户ID
     * @param password 密码
     * @return 结果
     */
    @Override
    public int resetUserPwd( Long userId, String password) {
        //        加密
        String passwordE = jmService.getEncryptString(password);
        //        修改密码修改时间
        return personMapper.resetUserPwd(userId, passwordE, DateUtils.getTime());
    }

    /**
     * 校验人员编号是否唯一
     *
     * @param empNum 人员编号
     * @return 结果
     */
    @Override
    public String checkEmpNumUnique(String empNum) {

        Integer resCont = personMapper.checkEmpNumUnique(empNum);

        if (resCont > 0) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }


    //加密
    private Person getEPerson(Person person) {
        if (person != null) {
            if (person.getPersonName() != null) {
                person.setPersonName(jmService.getEncryptString(person.getPersonName()));
            }

            if (person.getNameAp() != null) {
                person.setNameAp(jmService.getEncryptString(person.getNameAp()));

            }
            if (person.getBirNum() != null) {
                person.setBirNum(jmService.getEncryptString(person.getBirNum()));

            }
            if (person.getProvincialUserId() != null) {
                person.setProvincialUserId(jmService.getEncryptString(person.getProvincialUserId()));

            }
            if (person.getLoginAccount() != null) {
                person.setLoginAccount(jmService.getEncryptString(person.getLoginAccount()));

            }
            if (person.getLoginPsw() != null) {
                person.setLoginPsw(jmService.getEncryptString(person.getLoginPsw()));

            }
        }


        return person;
    }

    //    解密
    private Person getDPerson(Person person) {
        if (person != null) {
            if (person.getPersonName() != null) {
                person.setPersonName(jmService.getDecryptString(person.getPersonName()));
            }

            if (person.getNameAp() != null) {
                person.setNameAp(jmService.getDecryptString(person.getNameAp()));

            }
            if (person.getBirNum() != null) {
                person.setBirNum(jmService.getDecryptString(person.getBirNum()));

            }
            if (person.getProvincialUserId() != null) {
                person.setProvincialUserId(jmService.getDecryptString(person.getProvincialUserId()));

            }
            if (person.getLoginAccount() != null) {
                person.setLoginAccount(jmService.getDecryptString(person.getLoginAccount()));

            }
            if (person.getLoginPsw() != null) {
                person.setLoginPsw(jmService.getDecryptString(person.getLoginPsw()));
            }
        }

        return person;
    }
}
